package cn.com.darly.videoandroidpm.widget.callbutton;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import cn.com.darly.videoandroidpm.R;

/**
 * 接听或者挂断视频通话的按钮
 * @author Darly/张宇辉/2018/5/29 14:39
 * @version 1.0/cn.com.darly.videoandroidpm.widget.callbutton
 */
public class CallToggleButton extends View implements GestureDetector.OnGestureListener {
    private Drawable leftSliderImg, rightSliderImg;
    private int leftSliderX, rightSliderX;
    private int slidersHeight, slidersWidth;
    private GestureDetector mGestures;
    private OnSliderTriggeredListener mTriggerListener;
    private boolean slidingLeftHandle, slidingRightHandle;
    private static final double mCoeff = 0.5;
    private int once;

    public CallToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestures = new GestureDetector(getContext(), this);
        leftSliderImg = getResources().getDrawable(R.drawable.ic_start_call);
        rightSliderImg = getResources().getDrawable(R.drawable.ic_stop_call);

        slidersHeight = leftSliderImg.getIntrinsicHeight();
        slidersWidth = leftSliderImg.getIntrinsicWidth();

        leftSliderX = 0;
        rightSliderX = 0;
        slidingLeftHandle = slidingRightHandle =  false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        rightSliderImg.setBounds(getWidth() - slidersWidth - rightSliderX, getHeight() - slidersHeight, getWidth(), getHeight());
        rightSliderImg.draw(canvas);

        leftSliderImg.setBounds(0, getHeight() - slidersHeight, slidersWidth + leftSliderX, getHeight());
        leftSliderImg.draw(canvas);

        if (slidingLeftHandle && Math.abs(leftSliderX) >= mCoeff * getWidth()) {
            if (once == 0) {
                mTriggerListener.LeftEvent();
                once++;
            }

        } else if (slidingRightHandle && rightSliderX >= mCoeff * getWidth()) {
            if (once == 0) {
                mTriggerListener.RightEvent();
                once++;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            leftSliderX = 0;
            rightSliderX = 0;
            once = 0;
            slidingLeftHandle = slidingRightHandle = false;
            invalidate();
        }

        return mGestures.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        if (e1.getY() < getHeight() - slidersHeight) {
            return false;
        }

        if (e1.getX() < getWidth() / 2) {
            leftSliderX -= distanceX;
            slidingLeftHandle = true;
        } else {
            rightSliderX += distanceX;
            slidingRightHandle = true;
        }
        invalidate();

        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    public void setOnSliderTriggeredListener(OnSliderTriggeredListener listener) {
        mTriggerListener = listener;
    }


    /**
     * view接口方法
     */
    public interface OnSliderTriggeredListener {
          void RightEvent();
          void LeftEvent();
    }
}